﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using GalaSoft.MvvmLight.Messaging;
using serviceMaker.RssService;
using ViewModel.Rss;


namespace EPIClientRssWP7
{

    public partial class MasterHomeView : PhoneApplicationPage
    {
        RssViewModel vm;
        public MasterHomeView()
        {
            InitializeComponent();
            this.Loaded += (s, arg) =>
            {
                vm = this.DataContext as RssViewModel;
                Messenger.Default.Register<Uri>(this, "NavigationRequest", (paquet) =>
                {
                    NavigationService.Navigate(paquet);
                });

                Messenger.Default.Register<Uri>(this, "Logout", (uri) => 
                {
                    //while (NavigationService.CanGoBack)
                    //    NavigationService.RemoveBackEntry();
//                    NavigationService.GoBack();
                    NavigationService.Navigate(uri);
                });

              
                //Messenger.Default.Register<Uri>(this, "GoToAdd", (paquet) =>
                //{
                //    NavigationService.Navigate(paquet);
                //});

            };
        }

        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);
            if (vm != null)
            vm.RssSelected = null;
        }

        private void click_addField(object sender, System.EventArgs e)
        {
            NavigationService.Navigate(new Uri("/View/AddRssView.xaml", UriKind.Relative));
        }

        protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e)
        {
            while (NavigationService.CanGoBack == true)
                NavigationService.RemoveBackEntry();
            base.OnBackKeyPress(e);
        }


        private void Logout_click(object sender, System.EventArgs e)
        {
            if (vm is RssViewModel && vm != null)
            {
                if (vm.CanExecuteLogOut() == true)
                    vm.ExecuteLogOut();
            }
        }

        private void remove_feed(object sender, System.EventArgs e)
        {
        	 NavigationService.Navigate(new Uri("/View/RemoveRssView.xaml", UriKind.Relative));
        }

    }
}
